package com.ruoyi.data.service.impl;

import java.util.*;

import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.CreateIdUtils;
import com.ruoyi.common.utils.http.RestTemplateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.ruoyi.data.mapper.MerchantMapper;
import com.ruoyi.data.domain.Merchant;
import com.ruoyi.data.service.IMerchantService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 美团外卖商户Service业务层处理
 * 
 * @author shushuang
 * @date 2019-09-25
 */
@Service
public class MerchantServiceImpl implements IMerchantService
{
    @Resource
    private MerchantMapper merchantMapper;

    /**
     * 查询美团外卖商户
     * 
     * @param id 美团外卖商户ID
     * @return 美团外卖商户
     */
    @Override
    public Merchant selectMerchantById(String id)
    {
        return merchantMapper.selectMerchantById(id);
    }

    /**
     * 查询美团外卖商户列表
     * 
     * @param merchant 美团外卖商户
     * @return 美团外卖商户
     */
    @Override
    public List<Merchant> selectMerchantList(Merchant merchant)
    {
        return merchantMapper.selectMerchantList(merchant);
    }

    @Override
    public List<Merchant> selectMerchantList(Merchant merchant, Integer pageNum, Integer pageSize) {
        //爬虫获取

        //String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables
        String url = "https://meishi.meituan.com/i/api/channel/deal/list";
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");
        headers.put("Cookie","__mta=209558829.1570543677550.1570543677550.1570543677550.1; wm_order_channel=mtib; utm_source=60030; _lxsdk_cuid=16d9bfe49d31-08d9f559920a78-67e1b3f-e1000-16d9bfe49d4c8; iuuid=762B79399405F7AB36B8855A0193E5DB9DCAEDBFB6251A0F1D82D9DD2A34126A; token=0uflLnEwBi_TLYIsbS8tRid9XpIAAAAAPAkAAJER5lreJNhY4JXqAC-Iyo2-1bDJ4haXY1giR1RLnFAAe2nxGJSl3ERAD0LUQOmWYA; mt_c_token=0uflLnEwBi_TLYIsbS8tRid9XpIAAAAAPAkAAJER5lreJNhY4JXqAC-Iyo2-1bDJ4haXY1giR1RLnFAAe2nxGJSl3ERAD0LUQOmWYA; oops=0uflLnEwBi_TLYIsbS8tRid9XpIAAAAAPAkAAJER5lreJNhY4JXqAC-Iyo2-1bDJ4haXY1giR1RLnFAAe2nxGJSl3ERAD0LUQOmWYA; userId=839501539; _lxsdk=762B79399405F7AB36B8855A0193E5DB9DCAEDBFB6251A0F1D82D9DD2A34126A; au_trace_key_net=default; openh5_uuid=762B79399405F7AB36B8855A0193E5DB9DCAEDBFB6251A0F1D82D9DD2A34126A; uuid=762B79399405F7AB36B8855A0193E5DB9DCAEDBFB6251A0F1D82D9DD2A34126A; cssVersion=44cc90f1; _lx_utm=utm_source%3D60030; client-id=daa51eab-e3cb-48a1-832c-71689cf37a6f; ci=1; meishi_ci=1; cityid=1; _hc.v=8d8bfa30-3414-08ec-22bc-ead281f6495c.1570543677; logan_session_token=dxu3dp83oby3xy6a5chf; logan_custom_report=; _lxsdk_s=16daba6217f-d95-cd-932%7C%7C18");
        headers.put("Origin","https://meishi.meituan.com");
        headers.put("Referer","https://meishi.meituan.com/i/?ci=1&stid_b=1&cevent=imt%2Fhomepage%2Fcategory1%2F1");
        headers.put("Sec-Fetch-Mode","cors");
        headers.put("Sec-Fetch-Site","same-origin");
        MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("uuid","762B79399405F7AB36B8855A0193E5DB9DCAEDBFB6251A0F1D82D9DD2A34126A");
        requestBody.add("version","8.2.0");
        requestBody.add("platform","3");
        requestBody.add("app","");
        requestBody.add("partner",126);
        requestBody.add("riskLevel",1);
        requestBody.add("optimusCode",10);
        requestBody.add("originUrl","http://meishi.meituan.com/i/?ci=1&stid_b=1&cevent=imt%2Fhomepage%2Fcategory1%2F1");
        requestBody.add("offset",pageNum-1);
        requestBody.add("limit",pageSize);
        requestBody.add("cateId",1);
        requestBody.add("lineId",0);
        requestBody.add("stationId",0);
        requestBody.add("areaId",0);
        requestBody.add("sort","default");
        requestBody.add("deal_attr_23","");
        requestBody.add("deal_attr_24","");
        requestBody.add("deal_attr_25","");
        requestBody.add("poi_attr_20043","");
        requestBody.add("poi_attr_20033","");
        Map<String, ?> uriVariables = new HashMap<>();

        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url,headers,requestBody,JSONObject.class,uriVariables);
        JSONObject data = response.getBody().getObj("data");
        JSONObject.JSONArray arr = response.getBody().getObj("data").getObj("poiList").getArr("poiInfos");
        List<Merchant> list = new ArrayList<Merchant>();
        for (int i = 0; i < arr.size(); i++) {
            Object o = arr.get(i);
            Map<String, Object> map = (HashMap<String, Object>) o;
            Merchant mer = new Merchant();
            mer.setMerchantname(map.get("name").toString());
            mer.setMerchantaddress(map.get("areaName").toString());
            list.add(mer);
        }
        return list;
    }

    /**
     * 新增美团外卖商户
     * 
     * @param merchant 美团外卖商户
     * @return 结果
     */
    @Override
    public int insertMerchant(Merchant merchant)
    {
        merchant.setId(CreateIdUtils.getGenerateUUID());
        return merchantMapper.insertMerchant(merchant);
    }

    /**
     * 修改美团外卖商户
     * 
     * @param merchant 美团外卖商户
     * @return 结果
     */
    @Override
    public int updateMerchant(Merchant merchant)
    {
        return merchantMapper.updateMerchant(merchant);
    }

    /**
     * 删除美团外卖商户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMerchantByIds(String ids)
    {
        return merchantMapper.deleteMerchantByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除美团外卖商户信息
     * 
     * @param id 美团外卖商户ID
     * @return 结果
     */
    public int deleteMerchantById(String id)
    {
        return merchantMapper.deleteMerchantById(id);
    }
}
